<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="os-fd-ops.html" />
<link rel="prev" href="os-procinfo.html" />
<link rel="parent" href="module-os.html" />
<link rel="next" href="os-fd-ops.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>14.1.2 File Object Creation </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.1.1 process Parameters"
  href="os-procinfo.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.1 os  "
  href="module-os.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.1.3 file Descriptor Operations"
  href="os-fd-ops.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="os-procinfo.html">14.1.1 Process Parameters</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-os.html">14.1 os  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="os-fd-ops.html">14.1.3 File Descriptor Operations</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0016120000000000000000"></a><a name="os-newstreams"></a>
<br>
14.1.2 File Object Creation 
</h2>

<p>
These functions create new file objects.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2624' xml:id='l2h-2624' class="function">fdopen</tt></b>(</nobr></td>
  <td><var>fd</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return an open file object connected to the file descriptor <var>fd</var>.
<a id='l2h-2630' xml:id='l2h-2630'></a>
The <var>mode</var> and <var>bufsize</var> arguments have the same meaning as
the corresponding arguments to the built-in <tt class="function">open()</tt>
function.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.

<p>

<span class="versionnote">Changed in version 2.3:
When specified, the <var>mode</var> argument must now start
  with one of the letters "<tt class="character">r</tt>", "<tt class="character">w</tt>", or "<tt class="character">a</tt>",
  otherwise a <tt class="exception">ValueError</tt> is raised.</span>

<span class="versionnote">Changed in version 2.5:
On <span class="Unix">Unix</span>, when the <var>mode</var> argument starts with
  "<tt class="character">a</tt>", the <var>O_APPEND</var> flag is set on the file descriptor
  (which the <tt class="cfunction">fdopen()</tt> implementation already does on most
  platforms).</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2625' xml:id='l2h-2625' class="function">popen</tt></b>(</nobr></td>
  <td><var>command</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Open a pipe to or from <var>command</var>.  The return value is an open
file object connected to the pipe, which can be read or written
depending on whether <var>mode</var> is <code>'r'</code> (default) or <code>'w'</code>.
The <var>bufsize</var> argument has the same meaning as the corresponding
argument to the built-in <tt class="function">open()</tt> function.  The exit status of
the command (encoded in the format specified for <tt class="function">wait()</tt>) is
available as the return value of the <tt class="method">close()</tt> method of the file
object, except that when the exit status is zero (termination without
errors), <code>None</code> is returned.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.

<p>
The <tt class="module">subprocess</tt> module provides more powerful facilities for
spawning new processes and retrieving their results; using that module
is preferable to using this function.

<p>

<span class="versionnote">Changed in version 2.0:
This function worked unreliably under Windows in
  earlier versions of Python.  This was due to the use of the
  <tt class="cfunction">_popen()</tt> function from the libraries provided with
  Windows.  Newer versions of Python do not use the broken
  implementation from the Windows libraries.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2626' xml:id='l2h-2626' class="function">tmpfile</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return a new file object opened in update mode ("<tt class="samp">w+b</tt>").  The file
has no directory entries associated with it and will be automatically
deleted once there are no file descriptors for the file.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>

<p>
There are a number of different <tt class="function">popen*()</tt> functions that
provide slightly different ways to create subprocesses.  Note that the
<tt class="module">subprocess</tt> module is easier to use and more powerful;
consider using that module before writing code using the
lower-level <tt class="function">popen*()</tt> functions.

<p>
For each of the <tt class="function">popen*()</tt> variants, if <var>bufsize</var> is
specified, it specifies the buffer size for the I/O pipes.
<var>mode</var>, if provided, should be the string <code>'b'</code> or
<code>'t'</code>; on Windows this is needed to determine whether the file
objects should be opened in binary or text mode.  The default value
for <var>mode</var> is <code>'t'</code>.

<p>
Also, for each of these variants, on <span class="Unix">Unix</span>, <var>cmd</var> may be a sequence, in
which case arguments will be passed directly to the program without shell
intervention (as with <tt class="function">os.spawnv()</tt>). If <var>cmd</var> is a string it will
be passed to the shell (as with <tt class="function">os.system()</tt>).

<p>
These methods do not make it possible to retrieve the exit status from
the child processes.  The only way to control the input and output
streams and also retrieve the return codes is to use the
<tt class="class">Popen3</tt> and <tt class="class">Popen4</tt> classes from the <tt class="module"><a href="module-popen2.html">popen2</a></tt>
module; these are only available on <span class="Unix">Unix</span>.

<p>
For a discussion of possible deadlock conditions related to the use
of these functions, see ``<a class="ulink" href="popen2-flow-control.html"
  >Flow Control
Issues</a>''
(section&nbsp;<a href="popen2-flow-control.html#popen2-flow-control">17.4.2</a>).

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2627' xml:id='l2h-2627' class="function">popen2</tt></b>(</nobr></td>
  <td><var>cmd</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Executes <var>cmd</var> as a sub-process.  Returns the file objects
<code>(<var>child_stdin</var>, <var>child_stdout</var>)</code>.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.

<span class="versionnote">New in version 2.0.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2628' xml:id='l2h-2628' class="function">popen3</tt></b>(</nobr></td>
  <td><var>cmd</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Executes <var>cmd</var> as a sub-process.  Returns the file objects
<code>(<var>child_stdin</var>, <var>child_stdout</var>, <var>child_stderr</var>)</code>.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.

<span class="versionnote">New in version 2.0.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2629' xml:id='l2h-2629' class="function">popen4</tt></b>(</nobr></td>
  <td><var>cmd</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Executes <var>cmd</var> as a sub-process.  Returns the file objects
<code>(<var>child_stdin</var>, <var>child_stdout_and_stderr</var>)</code>.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.

<span class="versionnote">New in version 2.0.</span>

</dl>

<p>
(Note that <code><var>child_stdin</var>, <var>child_stdout</var>, and
<var>child_stderr</var></code> are named from the point of view of the child
process, so <var>child_stdin</var> is the child's standard input.)

<p>
This functionality is also available in the <tt class="module"><a href="module-popen2.html">popen2</a></tt> module
using functions of the same names, but the return values of those
functions have a different order.

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.1.1 process Parameters"
  href="os-procinfo.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.1 os  "
  href="module-os.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.1.3 file Descriptor Operations"
  href="os-fd-ops.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="os-procinfo.html">14.1.1 Process Parameters</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-os.html">14.1 os  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="os-fd-ops.html">14.1.3 File Descriptor Operations</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
